﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
//using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace TerenXNA
{
    public class TextViewer
    {
        public SpriteFont Font1;
        public Vector2 position1, position2;
        public SpriteBatch spriteBatch; //potrzebne do wyświetlania wszelakiego tekstu

        public static bool displayGUI;
        private bool displayGUIKeyPressed;
        public static int accuracy; //z jaką dokładnością wypisywać współrzędne
        private bool displayHeight; //wyświetlanie wysokości (m n.p.m.)

        public TextViewer()
        {
            accuracy = 2;
            displayHeight = true;

            // Create a new SpriteBatch, which can be used to draw textures.
            Font1 = Game1.contentManager.Load<SpriteFont>("Arial");          
            //FontPos = new Vector2(Game1.device.Viewport.Width / 2, Game1.device.Viewport.Height / 2);
            position1 = new Vector2(480, 20);
            position2 = new Vector2(480, 50);
            spriteBatch = new SpriteBatch(Game1.device);

            displayGUI = displayGUIKeyPressed = false;

            CoordinateConverter converter = new CoordinateConverter();
        }

        public virtual void keyUpdate(KeyboardState keyboardState)
        {
            if (keyboardState.IsKeyDown(Keys.G))
            {
                if (!displayGUIKeyPressed)
                {
                    displayGUI = !displayGUI;
                }
                displayGUIKeyPressed = true;
            }
            else
            {
                displayGUIKeyPressed = false;
            }
        }

        public void draw(double x, double y, float height, int tilesX, int tilesY, int tileWidth, int tileHeight)
        {
            if (displayGUI)
            {
                spriteBatch.Begin();

                int[] tempResults = new CoordinateConverter().meters2degrees(x, y);

                string[] outputs = new string[4];
                outputs[0] = "" + (int)(tempResults[1]);
                outputs[1] = "" + (int)(tempResults[2]);
                outputs[2] = "" + (int)(tempResults[4]);
                outputs[3] = "" + (int)(tempResults[5]);
                //foreach (string str in outputs)
                for (int i = 0; i < 4; i++)
                {
                    //int tmp = Convert.ToInt16(str);
                    int tmp = Convert.ToInt16(outputs[i]);
                    if (tmp < 10)
                    {
                        //str.Insert(0, "0");
                        outputs[i] = "0" + outputs[i];
                    }
                }

                // wynik wypisywany na ekran
                string output1 = "";
                if (accuracy == 0)
                {
                    output1 = (int)tempResults[0] + "° N   " + (int)tempResults[3] + "° E";
                }
                else if (accuracy == 1)
                {
                    output1 = (int)tempResults[0] + "° " + outputs[0] + "' N   " + (int)tempResults[3] + "° " + outputs[2] + "' E";
                }
                else
                {
                    output1 = (int)tempResults[0] + "° " + outputs[0] + "' " + outputs[1] + "\" N   " + (int)tempResults[3] + "° " + outputs[2] + "' " + outputs[3] + "\" E";
                }

                string output2 = height + "m n.p.m.";

                // Find the center of the string
                Vector2 Font1Origin = Font1.MeasureString(output1) / 2;
                // Draw the string
                spriteBatch.DrawString(Font1, output1, position1, Color.White, 0, Font1Origin, 1.0f, SpriteEffects.None, 0.5f);

                if (displayHeight)
                {
                    spriteBatch.DrawString(Font1, output2, position2, Color.White, 0, Font1Origin, 1.0f, SpriteEffects.None, 0.5f);
                }

                spriteBatch.End();

                // przywrócenie niektórych opcji zepsutych przez SpriteBatch
                Game1.device.BlendState = BlendState.Opaque;
                Game1.device.DepthStencilState = DepthStencilState.Default;
                //Game1.device.SamplerStates[0] = SamplerState.LinearWrap;
            }
        }

        public void changeAccuracy(int newAccuracy)
        {
            accuracy = newAccuracy;
        }

        public void toggleHeightDisplay()
        {
            displayHeight = !displayHeight;
        }
    }
}
